package com.ymatou.iapi.hunter.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.OnBindingTempRelationEventRequest;
import com.ymatou.iapi.hunter.service.OnBindingTempRelationEventCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterRelationTemp;
import com.ymttest.database.sqlwapper.hunter.HunterRelationTempWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_OnBindingTempRelationEvent {
	
	private static OnBindingTempRelationEventRequest onBindingTempRelationEventRequest;
	private static OnBindingTempRelationEventCall onBindingTempRelationEventCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("未注册前绑定临时用户关系");
	}

	@Before
	public void caseUp() {
		onBindingTempRelationEventRequest = new OnBindingTempRelationEventRequest();
		onBindingTempRelationEventCall = new OnBindingTempRelationEventCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(onBindingTempRelationEventCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(OnBindingTempRelationEventRequest req) {
		
		HunterRelationTempWrapper wrapper = new HunterRelationTempWrapper();
		HunterRelationTemp hunterRelationTemp = wrapper.selectByPrimaryKey(req.getWeixinId());
		
		Logger.verifyEquals(req.getWeixinId(),
				hunterRelationTemp.getWeixinId(), "校验WeixinId");
		Logger.verifyEquals(req.getHunterId(),
				hunterRelationTemp.getHunter_id(), "校验HunterId");
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnBindingTempRelationEvent_001() {
		Logger.start(true, "绑定临时关系");
		try {
			//任意输入hunter，不校验hunter是否存在
			long hunterId = 2221234L;
			
			onBindingTempRelationEventRequest.setHunterId(hunterId);
			onBindingTempRelationEventRequest.setWeixinId(TestDataManager.randomStr(13));

			onBindingTempRelationEventCall.setData(onBindingTempRelationEventRequest);
			onBindingTempRelationEventCall.callService();
			
			Logger.verifyEquals("ok", onBindingTempRelationEventCall.getCode(),
					"验证code");
			
			verifyDb(onBindingTempRelationEventRequest);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnBindingTempRelationEvent_002() {
		Logger.start(true, "weixinid为空");
		try {
			//任意输入hunter，不校验hunter是否存在
			long hunterId = 2221234L;
			
			onBindingTempRelationEventRequest.setHunterId(hunterId);
			onBindingTempRelationEventRequest.setWeixinId("");

			onBindingTempRelationEventCall.setData(onBindingTempRelationEventRequest);
			onBindingTempRelationEventCall.callService();
			
			Logger.verifyEquals("fail", onBindingTempRelationEventCall.getCode(),
					"验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OnBindingTempRelationEvent_003() {
		Logger.start(true, "hunterId为空");
		try {
			//任意输入hunter，不校验hunter是否存在
			long hunterId = 0;
			
			onBindingTempRelationEventRequest.setHunterId(hunterId);
			onBindingTempRelationEventRequest.setWeixinId("");

			onBindingTempRelationEventCall.setData(onBindingTempRelationEventRequest);
			onBindingTempRelationEventCall.callService();
			
			Logger.verifyEquals("fail", onBindingTempRelationEventCall.getCode(),
					"验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
